// pages/queue/changeMode/changeMode.js
const VEH = require("../../../interfaces/vehicles")
const IFS = require("../../../interfaces/interface")
const ORDER = require("../../../interfaces/order")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    chargeCarID: "",
    chargeConduct: false,
    chargeMode: "F",
    chargeAmount: 0,
    currentPower: 0,
    currentIndex:0,
    currentCapacity:0,
    fastColor: "#38608F",
    slowColor: "#9CB9DB",
    curOrder:0,
    newChargeMode: "F",
    newChargeAmount: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      curOrder: ORD.getCurrentOrder(),
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  /*选择待充电的车id*/
  onConfirm(e) {
    this.setData({
      chargeCarID: e.currentTarget.dataset.tid
    })
  },

  onTapFastChargeBtn() {
    var chosenColor = '#38608F'
    var unchosenColor = '#9CB9DB'
    if(this.data.fastColor!=chosenColor&&this.data.slowColor==chosenColor)
    {
      this.setData({
      fastColor: chosenColor,
      slowColor: unchosenColor,
      chargeMode: "F"
      });
    }
  },

  onTapSlowChargeBtn() {
    var chosenColor = '#38608F'
    var unchosenColor = '#9CB9DB'
    if(this.data.slowColor!=chosenColor&&this.data.fastColor==chosenColor)
    {
      this.setData({
      fastColor: unchosenColor,
      slowColor: chosenColor,
      chargeMode: "T"
      });
    }
  },
  onInputAmount(e) {
    this.setData({
      chargeAmount: e.detail.value
    })
  },
  onInputCurrentPower(e) {
    this.setData({
      currentPower: e.detail.value
    })
  },

  // 请实现下面四个接口
  getChargeCarID()
  {
      return this.data.chargeCarID
  },
  getChargeMode()
  {
    return this.data.chargeMode
  },
  getChargeAmount()
  {
    return this.data.chargeAmount
  },
  check(id,mode,amount)
  {
    console.log(id,mode,amount)
    return (mode=="T"||mode=="F")&&amount>0
  },

  onSlideChange(e)
  {
    var that = this;
    let cma = this.data.currentCapacity - this.data.currentPower;
    that.setData({
      currentIndex:e.detail.current,
      chargeCarID:this.data.userVehicles[e.detail.current].car_id,
      currentCapacity:this.data.userVehicles[e.detail.current].power_capacity
    })
  },
  onSlideKwh(e)
  {
    this.setData({
      chargeAmount:e.detail.value
    })
  },
  
  async onSubmitBtn(e)
  {
    let id = this.getChargeCarID()
    let mode = this.getChargeMode()
    let amount = this.getChargeAmount()
    if(!this.check(id,mode,amount))
    {
      wx.showModal({
        title:"请检查输入"
      })
      return
    }
    
    // 显示确认信息，在用户点击确认后提交，否则return
    let result = await IFS.createOrder(id,mode,amount)
    if (result.status=="success")
    {
      let get_response = await getApp().startCyclicQueryOrder()
      if(!get_response)
      {
        // 这里的逻辑还没想好，理论上创建了订单但是查不到当前订单也是应该在后台一直查的
        // 可以考虑回到首页
        wx.navigateBack({
          delta: getCurrentPages().length-1,
        })
        wx.showModal({
          title:"服务器错误",
          content:"可稍后重试以查询排队/充电情况"
        })
      }
      else{
        // 否则根据订单状态考虑跳转至充电页还是排队页
        let order = ORDER.getCurrentOrder()
        if(order.state=="WAITING"||order.state=="DISPATCHED")
        {
          wx.navigateTo({
            url: '../queue/queue',
            success:(res)=>{
              console.log("success")
            },
            fail:(res)=>{
              console.log(res)
            }
          })
        }
        else{
          wx.navigateTo({
            url: '../charging/charging',
            success:(res)=>{
              console.log("success")
            },
            fail:(res)=>{
              console.log(res)
            }
          })
        }
      }
    }
    else{
      console.log(result)
      wx.showToast({
        title: '提交失败',
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
  }
})